package com.lxu.demo.tree;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MenuUtil {

    public static void main(String[] args) {
        List<Menu> menus = generateMenuList();

        //Menu tree = combineToMenuTree(menus);

        //Menu tree2 = combineToMenuTree2(menus);

        Menu tree3 = combineToMenuTree3(menus);

        //System.out.println(tree);
        System.out.println(tree3);
    }

    private static Menu combineToMenuTree3(List<Menu> menus) {
        Menu menu = getRootMenu(menus);
        Map<String,Menu> rootMap = new HashMap<>();
        Map<String,Menu> nodeMap = new HashMap<>();
        rootMap.put(menu.getId(),menu);
        menus.forEach(menu1 -> {
            nodeMap.put(menu1.getId(),menu1);
        });
        menus.forEach( menu1 -> {
            String parentId = menu1.getParentId();
            if(rootMap.containsKey(parentId)){
                rootMap.get(parentId).add(menu1);
            }else if(nodeMap.containsKey(parentId)){
                nodeMap.get(parentId).add(menu1);
            }
        });
        return menu;
    }

    private static Menu combineToMenuTree(List<Menu> menus) {
        Menu menu = getRootMenu(menus);
        return getChildrenOfTree(menu, menus);
    }

    private static Menu combineToMenuTree2(List<Menu> menus) {
        Menu menu = getRootMenu(menus);
        return getChildrenOfTree2(menu, menus);
    }

    private static Menu getChildrenOfTree(Menu menu, List<Menu> menus) {
        menu.setChildren(new ArrayList<>());
        for (Menu menu1 : menus) {
            if (StringUtils.isNotBlank(menu1.getParentId()) && menu1.getParentId().equals(menu.getId())) {
                menu.getChildren().add(getChildrenOfTree(menu1, menus));
            }
        }
        return menu;
    }

    private static Menu getChildrenOfTree2(Menu menu, List<Menu> menus) {
        Menu temp = new Menu();
        BeanUtils.copyProperties(menu,temp);
        temp.setChildren(new ArrayList<>());
        for (Menu menu1 : menus) {
            if (StringUtils.isNotBlank(menu1.getParentId()) && menu1.getParentId().equals(temp.getId())) {
                temp.getChildren().add(getChildrenOfTree(menu1, menus));
            }
        }
        return temp;
    }

    private static Menu getRootMenu(List<Menu> menus) {
        List<Menu> menuList = menus.stream().filter(menu -> {
            return "1".equals(menu.getId());
        }).collect(Collectors.toList());
        return menuList.get(0);
    }

    private static List<Menu> generateMenuList() {

        List<Menu> menus = new ArrayList<Menu>();

        Menu menu = new Menu();
        menu.setId("1");
        menu.setParentId(null);
        menu.setName("根节点");

        Menu menu01 = new Menu();
        menu01.setId("01");
        menu01.setParentId("1");
        menu01.setName("二级节点-1");

        Menu menu02 = new Menu();
        menu02.setId("02");
        menu02.setParentId("1");
        menu02.setName("二级节点-2");

        Menu menu001 = new Menu();
        menu001.setId("001");
        menu001.setParentId("01");
        menu001.setName("三级节点-1");

        Menu menu002 = new Menu();
        menu002.setId("002");
        menu002.setParentId("01");
        menu002.setName("三级节点-2");

        menus.add(menu);
        menus.add(menu01);
        menus.add(menu02);
        menus.add(menu001);
        menus.add(menu002);

        return menus;

    }
}
